import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreFileService extends BaseService<any> {
  private readonly logger = new Logger(CoreFileService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * setRootPath
   * 对应 PHP: CoreFileService_core::setRootPath()
   * 逻辑类型: undefined - undefined
   */
  async setRootPath(root_path: any) {
    // 基于PHP真实逻辑: setRootPath
    // PHP原文: $this->root_path = $root_path;         return $this;     }      /**      * 设置上传规则      * @param array $validate  ['ext' => [], 'mime' => [], 'size' =>...
this.root_path = root_path;
        return this;
    }

    /**
     * 设置上传规则
     * @param validate  [ext: [], mime: [], size: 0]
     * @return void
     */
    async setValidate(validate){
        this.validate = validate;
        return this;
    }

    /**
     * 设置重命名
     * @param rename
     * @return this
     */
    async setRename(rename = ''){
        this.rename = rename;
        return this;
    }

    /**
     * @var FileDriver
     */
    protected upload_driver;
    protected static storage_type;

    /**
     * 实例化上传引擎
     * @param int site_id
     * @param string storage_type `非空则是强制使用某种存储方式 为空则是走后台设置`
     * @return UploadLoader
     * @throws BusinessException
     */
    async driver(int site_id, string storage_type = ''){
        if(!storage_type)){
            const core_storage_service = this.coreStorageService;
            const storage_config = core_storage_service.getDefaultStorage(site_id);
            if(!storage_config)) throw new UploadFileBusinessException('UPLOAD_STORAGE_TYPE_ALL_CLOSE');
            //若获取到的存储类型为`默认` 则获取超级管理的配置
            if (storage_config.storage_type == StorageDict.LOCAL) {
                const storage_config = core_storage_service.getDefaultStorage(0);
                if(!storage_config)) throw new UploadFileBusinessException('SUPER_UPLOAD_STORAGE_TYPE_ALL_CLOSE');
            }
            self.storage_type = storage_config.storage_type;
  }

  /**
   * setValidate
   * 对应 PHP: CoreFileService_core::setValidate()
   * 逻辑类型: undefined - undefined
   */
  async setValidate(validate: any[]) {
    // 基于PHP真实逻辑: setValidate
    // PHP原文: $this->validate = $validate;         return $this;     }      /**      * 设置重命名      * @param $rename      * @return $this      */     public function ...
this.validate = validate;
        return this;
    }

    /**
     * 设置重命名
     * @param rename
     * @return this
     */
    async setRename(rename = ''){
        this.rename = rename;
        return this;
    }

    /**
     * @var FileDriver
     */
    protected upload_driver;
    protected static storage_type;

    /**
     * 实例化上传引擎
     * @param int site_id
     * @param string storage_type `非空则是强制使用某种存储方式 为空则是走后台设置`
     * @return UploadLoader
     * @throws BusinessException
     */
    async driver(int site_id, string storage_type = ''){
        if(!storage_type)){
            const core_storage_service = this.coreStorageService;
            const storage_config = core_storage_service.getDefaultStorage(site_id);
            if(!storage_config)) throw new UploadFileBusinessException('UPLOAD_STORAGE_TYPE_ALL_CLOSE');
            //若获取到的存储类型为`默认` 则获取超级管理的配置
            if (storage_config.storage_type == StorageDict.LOCAL) {
                const storage_config = core_storage_service.getDefaultStorage(0);
                if(!storage_config)) throw new UploadFileBusinessException('SUPER_UPLOAD_STORAGE_TYPE_ALL_CLOSE');
            }
            self.storage_type = storage_config.storage_type;
  }

  /**
   * setRename
   * 对应 PHP: CoreFileService_core::setRename()
   * 逻辑类型: undefined - undefined
   */
  async setRename(rename: any) {
    // 基于PHP真实逻辑: setRename
    // PHP原文: $this->rename = $rename;         return $this;     }      /**      * @var FileDriver      */     protected $upload_driver;     protected static $stora...
this.rename = rename;
        return this;
    }

    /**
     * @var FileDriver
     */
    protected upload_driver;
    protected static storage_type;

    /**
     * 实例化上传引擎
     * @param int site_id
     * @param string storage_type `非空则是强制使用某种存储方式 为空则是走后台设置`
     * @return UploadLoader
     * @throws BusinessException
     */
    async driver(int site_id, string storage_type = ''){
        if(!storage_type)){
            const core_storage_service = this.coreStorageService;
            const storage_config = core_storage_service.getDefaultStorage(site_id);
            if(!storage_config)) throw new UploadFileBusinessException('UPLOAD_STORAGE_TYPE_ALL_CLOSE');
            //若获取到的存储类型为`默认` 则获取超级管理的配置
            if (storage_config.storage_type == StorageDict.LOCAL) {
                const storage_config = core_storage_service.getDefaultStorage(0);
                if(!storage_config)) throw new UploadFileBusinessException('SUPER_UPLOAD_STORAGE_TYPE_ALL_CLOSE');
            }
            self.storage_type = storage_config.storage_type;
  }

  /**
   * driver
   * 对应 PHP: CoreFileService_core::driver()
   * 逻辑类型: undefined - undefined
   */
  async driver(site_id: number, storage_type: number) {
    // 基于PHP真实逻辑: driver
    // PHP原文: if(empty($storage_type)){             $core_storage_service = new CoreStorageService();             $storage_config = $core_storage_service->getDefaul...
if(!storage_type)){
            const core_storage_service = this.coreStorageService;
            const storage_config = core_storage_service.getDefaultStorage(site_id);
            if(!storage_config)) throw new UploadFileBusinessException('UPLOAD_STORAGE_TYPE_ALL_CLOSE');
            //若获取到的存储类型为`默认` 则获取超级管理的配置
            if (storage_config.storage_type == StorageDict.LOCAL) {
                const storage_config = core_storage_service.getDefaultStorage(0);
                if(!storage_config)) throw new UploadFileBusinessException('SUPER_UPLOAD_STORAGE_TYPE_ALL_CLOSE');
            }
            self.storage_type = storage_config.storage_type;
  }
}
